package com.cti.commons.delayqueue.handler;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.concurrent.ListenableFuture;

import lombok.extern.slf4j.Slf4j;

/**
 * @author et.wang
 * @description: 显示线程执行情况：任务总数、已完成数、活跃线程数，队列大小信息
 */
@Slf4j
public class VisiableThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {

    private void showThreadPoolInfo(String prefix) {
        ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
        if (null == threadPoolExecutor) {
            return;
        }

        //log.debug("{}{},核心线程数 [{}], 最大线程数 [{}], 当前线程数 [{}], 提交任务数 [{}], 完成任务数 [{}], 活跃线程数 [{}], 队列等待数 [{}]",
        //    this.getThreadNamePrefix(),
        //    prefix,
        //    threadPoolExecutor.getCorePoolSize(),
        //    threadPoolExecutor.getMaximumPoolSize(),
        //    threadPoolExecutor.getPoolSize(),
        //    threadPoolExecutor.getTaskCount(),
        //    threadPoolExecutor.getCompletedTaskCount(),
        //    threadPoolExecutor.getActiveCount(),
        //    threadPoolExecutor.getQueue().size());
    }

    @Override
    public void execute(Runnable task) {
        showThreadPoolInfo("1. do execute");
        super.execute(task);
    }

    @Override
    public void execute(Runnable task, long startTimeout) {
        showThreadPoolInfo("2. do execute");
        super.execute(task, startTimeout);
    }

    @Override
    public Future<?> submit(Runnable task) {
        showThreadPoolInfo("1. do submit");
        return super.submit(task);
    }

    @Override
    public <T> Future<T> submit(Callable<T> task) {
        showThreadPoolInfo("2. do submit");
        return super.submit(task);
    }

    @Override
    public ListenableFuture<?> submitListenable(Runnable task) {
        showThreadPoolInfo("1. do submitListenable");
        return super.submitListenable(task);
    }

    @Override
    public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
        showThreadPoolInfo("2. do submitListenable");
        return super.submitListenable(task);
    }

}